package com.ruoyi.project.system.app.controller;

import java.io.ByteArrayOutputStream;
import java.util.List;

import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.app.domain.CtcpApp;
import com.ruoyi.project.system.app.service.ICtcpAppService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * CTCP应用Controller
 * 
 * @author huzhenyuan
 * @date 2021-07-15
 */
@Controller
@RequestMapping("/system/app")
public class CtcpAppController extends BaseController
{
    private String prefix = "system/app";

    @Autowired
    private ICtcpAppService ctcpAppService;

    @RequiresPermissions("system:app:view")
    @GetMapping()
    public String app()
    {
        return prefix + "/app";
    }

    /**
     * 查询CTCP应用列表
     */
    @RequiresPermissions("system:app:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CtcpApp ctcpApp)
    {
        startPage();
        List<CtcpApp> list = ctcpAppService.selectCtcpAppList(ctcpApp);
        return getDataTable(list);
    }

    /**
     * 导出CTCP应用列表
     */
    @RequiresPermissions("system:app:export")
    @Log(title = "CTCP应用", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CtcpApp ctcpApp)
    {
        List<CtcpApp> list = ctcpAppService.selectCtcpAppList(ctcpApp);
        ExcelUtil<CtcpApp> util = new ExcelUtil<CtcpApp>(CtcpApp.class);
        return util.exportExcel(list, "CTCP应用数据");
    }

    /**
     * 新增CTCP应用
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存CTCP应用
     */
    @RequiresPermissions("system:app:add")
    @Log(title = "CTCP应用", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CtcpApp ctcpApp)
    {
        return toAjax(ctcpAppService.insertCtcpApp(ctcpApp));
    }

    /**
     * 修改CTCP应用
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        CtcpApp ctcpApp = ctcpAppService.selectCtcpAppById(id);
        mmap.put("ctcpApp", ctcpApp);
        return prefix + "/edit";
    }

    /**
     * 修改保存CTCP应用
     */
    @RequiresPermissions("system:app:edit")
    @Log(title = "CTCP应用", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CtcpApp ctcpApp)
    {
        return toAjax(ctcpAppService.updateCtcpApp(ctcpApp));
    }

    /**
     * 删除CTCP应用
     */
    @RequiresPermissions("system:app:remove")
    @Log(title = "CTCP应用", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(ctcpAppService.deleteCtcpAppByIds(ids));
    }

    /**
     * 修改保存CTCP应用
     */
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id, ModelMap mmap)
    {
        CtcpApp ctcpApp = ctcpAppService.selectCtcpAppById(id);
        mmap.put("ctcpApp", ctcpApp);
        return prefix + "/detail";
    }

    @PostMapping("/submit")
    @ResponseBody
    public AjaxResult submit(@RequestParam(value="command", required=true) String command)
    {
        String username = "tee";
        String password = "tee";
        String host = "127.0.0.1";
        int port = 22;
        Session session = null;
        ChannelExec channel = null;

        try {
            session = new JSch().getSession(username, host, port);
            session.setPassword(password);
            session.setConfig("StrictHostKeyChecking", "no");
            session.connect();

            channel = (ChannelExec) session.openChannel("exec");
            channel.setCommand(command);
            ByteArrayOutputStream responseStream = new ByteArrayOutputStream();
            channel.setOutputStream(responseStream);
            channel.connect();

            while (channel.isConnected()) {
                Thread.sleep(100);
            }

            String responseString = new String(responseStream.toByteArray());
            System.out.println(responseString);
        } catch (InterruptedException | JSchException e) {
            e.printStackTrace();
        } finally {
            if (session != null) {
                session.disconnect();
            }
            if (channel != null) {
                channel.disconnect();
            }
        }
        return toAjax(1);
    }

}
